from qturboframe.lib.report.log import logger
from qturboframe.lib.webdriver.webdriver_factory import WebDriverFactory
from qturboframe.lib.common.web_config import web_config, web_config_secondary
from qturboframe.lib.common.__script_config__ import script_config
from qturboframe.lib.utils.find_vue import get_vue_list


def binding_menu_vue(func):
    def wrapper(self, link_text):
        # 查找driver_id
        web_fac_temp = WebDriverFactory(browser_id='')
        driver_list = web_fac_temp.get_driver_list()
        driver_key = ''
        for driver_map in driver_list:
            key = list(driver_map.keys())[0]
            if self._driver == driver_map.get(key):
                driver_key = key
                break
        # 执行菜单选择
        func(self, link_text)
        value_list = link_text.split('/')
        # 判断当前driver是否使用了代理
        web_fac = WebDriverFactory(browser_id=driver_key)
        if web_fac.is_proxy():
            # 获取当前菜单相关vue
            proxy_har = web_fac.get_proxy_har()
            if proxy_har:
                vue_list = get_vue_list(proxy_har)
            else:
                vue_list = []
            # 缓存菜单vue关联
            if value_list[-1] not in script_config.task_menu_related.get(driver_key):
                script_config.task_menu_related.get(driver_key)[value_list[-1]] = vue_list
            logger.debug('{}菜单页关联：{}'.format(value_list[-1], script_config.task_menu_related))
            # 缓存菜单名
            if driver_key == 'primary':
                web_config.last_menu = value_list[-1]
            else:
                web_config_secondary.last_menu = value_list[-1]
    return wrapper
